//
// Created by martin on 8/3/21.
//

#include <fcntl.h>
#include <sys/stat.h>
#include "utils.h"
#include "error.h"

ssize_t utils_getfilesize(int fd)
{
    struct stat st;
    if (fstat(fd, &st) < 0) {
        return -1;
    }

    return st.st_size;
}

int utils_setnonblocking(int fd)
{
    int old_opt;
    old_opt = fcntl(fd, F_GETFL);
    int new_opt = old_opt | O_NONBLOCK;
    if (fcntl(fd, F_SETFL, new_opt) < 0) {
        err_ret("fcntl F_SETFL error");
        return -1;
    }
    return old_opt;
}